//
//  ByMemberStoreVC.m
//  mijifen_ios
//
//  Created by mc on 2017/10/23.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByMemberStoreVC.h"
// cell
#import "ByCellMemberStorePhoneOrPrice.h"   // 手机号／储值金额
#import "ByCellMemberStoreType.h"           // 储值方式／消费类型

// vc
#import "ByMemberStoreSuccessVC.h"

// view
#import "ByViewMemberStoreMode.h"

@interface ByMemberStoreVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView           *tableView;

@property (nonatomic, copy)   NSArray               *titleAry;
@property (nonatomic, assign) BOOL                  isTime;             // 是否按次
@property (nonatomic, strong) NSMutableDictionary   *infoDic;
@end

@implementation ByMemberStoreVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"会员储值";
    [self addLeftBackBarButtonItem];
    [self initTableView];
}

#pragma mark - getter
- (NSMutableDictionary *)infoDic{
    if (!_infoDic) {
        _infoDic = [NSMutableDictionary dictionary];
    }
    return _infoDic;
}

#pragma mark - 初始化视图
- (void)initTableView{
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, self.view.height - 50 - self.view.safeAreaInsets.bottom));
        }else{
            make.top.equalTo(self.view.mas_top);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, self.view.height - 50));
        }
        make.left.equalTo(self.view.mas_left);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    
    UIButton *storeButton = [[UIButton alloc] init];
    [self.view addSubview:storeButton];
    [storeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.bottom.equalTo(self.view.mas_bottom);
        }
        make.left.equalTo(self.view.mas_left);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    [storeButton setTitle:@"储值" forState:UIControlStateNormal];
    [storeButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    [storeButton setBackgroundColor:BY_THEME_COLOR];
    [storeButton addTarget:self action:@selector(storeAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - getter
- (NSArray *)titleAry{
    if (!_titleAry) {
        _titleAry = @[@"手机号",@"储值金额",@"储值方式",@"消费类型",@"次数",@"储值优惠信息"];
    }
    return _titleAry;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __weak typeof(self) weakSelf = self;
    if (indexPath.section < 2 || indexPath.section == 4 || indexPath.section == 5) {
        ByCellMemberStorePhoneOrPrice *cell = [ByCellMemberStorePhoneOrPrice cellWithTableView:tableView andTitle:self.titleAry[indexPath.section]];
        if (indexPath.section == 4 && !self.isTime) {
            cell.hidden = YES;
        }else{
            cell.hidden = NO;
        }
        cell.editBlock = ^(NSString *str) {
            [weakSelf.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
        };
        return cell;
    }else{
        ByCellMemberStoreType *cell = [ByCellMemberStoreType cellWithTableView:tableView andTitle:self.titleAry[indexPath.section] andSection:indexPath.section];
        cell.consumeBlock = ^(NSString *str) {
            if ([str isEqualToString:@"按次消费"]) {
                weakSelf.isTime = YES;
            }else{
                weakSelf.isTime = NO;
            }
            [weakSelf.infoDic setValue:str forKey:[NSString stringWithFormat:@"%ld",indexPath.section + 10]];
            NSIndexSet *set = [NSIndexSet indexSetWithIndex:4];
            [weakSelf.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationNone];
        };
//        __weak typeof(cell) weakCell = cell;
//        cell.clickBlock = ^{
//              ByViewMemberStoreMode  *modeView = [[ByViewMemberStoreMode alloc] init];
//                if (indexPath.section == 2) {
//                    modeView.modeArray = @[@"微信",@"支付宝",@"银行卡",@"余额"];
//                }else{
//                    modeView.modeArray = @[@"按次消费",@"按月消费",@"按年消费"];
//                }
//                modeView.didSelectBlock = ^(NSString *str) {
//                    weakCell.contentLabel.text = str;
//                };
//                [modeView show];
//        };
        return cell;
    }
   
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        CGFloat x = [@"储值优惠信息" getSizeOfString:16].width + 30 + 20;
        UILabel *balanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, 5, SCREEN_WIDTH - x, 20)];
        balanceLabel.text = [NSString stringWithFormat:@"余额：%@",@"0"];
        balanceLabel.textColor = [UIColor darkGrayColor];
        balanceLabel.font= FONT(16);
        [footerView addSubview:balanceLabel];
        footerView.backgroundColor = [UIColor clearColor];
        return footerView;
    }else if (section == 5){
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
        CGFloat width = [@"储值优惠信息" getSizeOfString:16].width ;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5,width, 20)];
        titleLabel.textColor = [UIColor darkGrayColor];
        titleLabel.font= FONT(16);
        titleLabel.text = @"（选填）";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [footerView addSubview:titleLabel];
        footerView.backgroundColor = [UIColor clearColor];
        return footerView;
    }
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0 || section == 5) {
        return 30;
    }
    return 0.001;
}

#pragma mark -
- (void)storeAction:(UIButton *)button{
    NSString *phone = self.infoDic[@"10"];
    if (phone.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入手机号"];
        return;
    }
    NSString *price = self.infoDic[@"11"];
    if (price.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入储值金额"];
        return;
    }
    NSString *type = self.infoDic[@"12"];
    if (type.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请选择储值方式"];
        return;
    }
    NSString *consume = self.infoDic[@"13"];
    if (consume.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请选择消费类型"];
        return;
    }
    NSString *time = self.infoDic[@"14"];
    NSString *discount = self.infoDic[@"15"];
    ByMemberStoreSuccessVC *vc = [[ByMemberStoreSuccessVC alloc] init];
    vc.price = price;
    vc.phone = phone;
    [self by_normalPush:vc hasNavi:YES];
}



@end
